<?php 
## include required files
/*******************************/
require_once '../model/admindetails.php';
require_once '../model/email.php';
/*******************************/

## Create Objects
/*******************************/
$userObj = new Model_AdminDetails();
$emailObj = new Model_Email();
/*******************************/
if(isset($_POST['username']) || isset($_POST['first_name'])) {
	## apply PHP validation for required filed
	if(trim($_POST['username']) != '' && trim($_POST['first_name']) != '' && trim($_POST['last_name']) != '' && trim($_POST['email']) != '') {
		
		## Check is username exist
		$username_result = $userObj->chkUserNameExist(trim($_POST['username']), $_POST['hidden_userid']);
		## Check is email address exist
		$email_result = $userObj->chkUserEmailExist(trim($_POST['email']), $_POST['hidden_userid']);
		
		if(count($username_result) > 0) {
			$_SESSION['msg'] = '<div class="error_msg"><span>Username already exist</span></div>';		
		} else if(count($email_result) > 0) {
			$_SESSION['msg'] = '<div class="error_msg"><span>Email already exist</span></div>';		
		} else {
			$userArray = array();
			extract($_POST);
			$user_id = $_POST['hidden_userid'];
			$userArray['username']   = return_post_value($username);
			$userArray['first_name']   = return_post_value($first_name);
			$userArray['last_name']   = return_post_value($last_name);
			$userArray['email']   = return_post_value($email);
			$userArray['modified_date']   = getCurDate();
			$affctcnt = $userObj->editUserValueById($userArray, $user_id);
			//print_R($affctcnt);
			if($affctcnt > 0)
			{
			// Send email to admin user login details
					## Fetch email content
					$emailArray = $emailObj->getEmailById(6);
					$subject = $emailArray['email_subject'];
					//$subject = str_replace('[SITENAME]', SITENAME, $subject);
					$message = $emailArray['email_content'];
					
					## Create message
					$message = str_replace('[NAME]', return_post_value($first_name)." ".return_post_value($last_name), $message);
					$message = str_replace('[USERNAME]',return_post_value($username) , $message);
					$message = str_replace('[EMAIL]',return_post_value($email) , $message);
					$message = str_replace('[SITENAME]', SITENAME, $message);
					$message = str_replace('[SITELINK]',SITE_URL , $message);
					
					
					$to = return_post_value($email);
					
					$from = $emailArray['from_email'];
					$fromname = FRONTTITLE;
					
					$emailTemplate = file_get_contents('../emailtemplate.html');
					
					$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
					$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/templates/default/images/logo.png">', $template_msg);
					$template_msg = str_replace('[MESSAGE]',$message , $template_msg);
		
					//echo $to."<br />".$template_msg."<br />".$subject."<br />".$from."<br />".$fromname;exit;
					if(trim($to) != '')
						@send_mail($to, $template_msg, $subject, $from, $fromname);
			}
		
			$_SESSION['msg'] = "<div class='success_msg'><span>Admin user updated successfully</span></div>";
			header('location:'.SITE_URL.'/admin/home.php?q=adminusers&page='.$_GET['page']);
			exit;
		}
	} else { // php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please fill all required fields</span></div>';
	}
	// main if closed
}

## Fetch admin user details by id
if($_GET['id'] != '' && $_GET['action'] == 'edit') {
	$user_id = base64_decode($_GET['id']);	
	$user_details = $userObj->getAdminUserDetailsByUserId($user_id);
	$smarty->assign('user_details', $user_details);
}

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'adminusers');
$smarty->assign('mainmenu', '4');
## Unset all the objects created which are on this page
unset($userObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'adminusers/edituser.tpl');
unset($smarty);
?>